package com.ruoyi.web.controller.module.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.CustomerStatic;
import com.ruoyi.customer.service.ICustomerStaticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户统计信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/customer/static")
public class CustomerStaticController extends BaseController
{
    @Autowired
    private ICustomerStaticService customerStaticService;

    /**
     * 获取客户统计列表
     */
    @PostMapping("/customerStaticList")
    public TableDataInfo list(@RequestBody CustomerStatic customerStatic, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            customerStatic.setShopId(shopId);
        }
        startPageByPost();
        List<CustomerStatic> list = customerStaticService.selectUserList(customerStatic);
        return getDataTable(list);
    }

    /**
     * 根据客户统计编号获取详细信息
     */
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        CustomerStatic customerStatic = customerStaticService.selectUserById(userId);
        ajax.put(AjaxResult.DATA_TAG, customerStatic);
        return ajax;
    }

    /**
     * 修改客户统计
     */
    @PreAuthorize("@ss.hasPermi('customer:user:edit')")
    @Log(title = "客户统计管理 - 修改", businessType = BusinessType.UPDATE)
    @PostMapping("/customerStaticUpdate")
    public AjaxResult edit(@Validated @RequestBody CustomerStatic user)
    {
        CustomerStatic es = new CustomerStatic();
        es.setUserId(user.getUserId());
        es.setUpdateBy(getUsername());
        return toAjax(customerStaticService.updateUser(es));
    }

    @Log(title = "客户统计管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('customer:user:export')")
    @PostMapping("/customerStaticExport")
    public void export(HttpServletResponse response, CustomerStatic user)
    {
        List<CustomerStatic> list = customerStaticService.selectUserList(user);
        ExcelUtil<CustomerStatic> util = new ExcelUtil<CustomerStatic>(CustomerStatic.class);
        util.exportExcel(response, list, "客户统计数据");
    }
}
